package com.wzy.new_energy_car_platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wzy.new_energy_car_platform.common.GlobalVariable;
import com.wzy.new_energy_car_platform.common.ResultDto;
import com.wzy.new_energy_car_platform.dao.CarMapper;
import com.wzy.new_energy_car_platform.dao.CarModelMapper;
import com.wzy.new_energy_car_platform.dao.CarTypeMapper;
import com.wzy.new_energy_car_platform.dao.UserCollectMapper;
import com.wzy.new_energy_car_platform.model.*;
import com.wzy.new_energy_car_platform.service.CarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.new_energy_car_platform.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wzy
 * @since 2024-03-28
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {
    @Autowired
    private CarMapper carMapper;

    @Autowired
    private CarModelMapper carModelMapper;

    @Autowired
    private CarTypeMapper carTypeMapper;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private UserCollectMapper userCollectMapper;

    @Override
    public ResultDto<?> getCarList(String keyword,String brand,String priceRange,String level,String energy,int page,int pageSize) {
        LambdaQueryWrapper<Car> carLambdaQueryWrapper = new LambdaQueryWrapper<>();
        carLambdaQueryWrapper.select(Car::getId,Car::getName,Car::getMinPrice,Car::getMaxPrice,Car::getImg,Car::getSalesNum)
                .like(StringUtils.hasText(keyword),Car::getName,keyword)
                .eq(Car::getValidFlag, GlobalVariable.VALID_TRUE);
        if (Objects.nonNull(brand) && !"".equals(brand)){
            carLambdaQueryWrapper.eq(Car::getBrand,brand);
        }
        if (Objects.nonNull(level) && !"".equals(level)){
            carLambdaQueryWrapper.eq(Car::getLevel,level);
        }
        if (Objects.nonNull(energy) && !"".equals(energy)){
            carLambdaQueryWrapper.eq(Car::getEnergy,energy);
        }
        if (Objects.nonNull(priceRange) && !"".equals(priceRange)){
            switch (priceRange){
                case "0-10万":carLambdaQueryWrapper.and(q->q.between(Car::getMinPrice,0,10).or().between(Car::getMaxPrice,0,10));break;
                case "10-15万":carLambdaQueryWrapper.and(q->q.between(Car::getMinPrice,10,15).or().between(Car::getMaxPrice,10,15));break;
                case "15-20万":carLambdaQueryWrapper.and(q->q.between(Car::getMinPrice,15,20).or().between(Car::getMaxPrice,15,20));break;
                case "20-25万":carLambdaQueryWrapper.and(q->q.between(Car::getMinPrice,20,25).or().between(Car::getMaxPrice,20,25));break;
                case "25-30万":carLambdaQueryWrapper.and(q->q.between(Car::getMinPrice,25,30).or().between(Car::getMaxPrice,25,30));break;
                case "30-40万":carLambdaQueryWrapper.and(q->q.between(Car::getMinPrice,30,40).or().between(Car::getMaxPrice,30,40));break;
                case "50万以上":carLambdaQueryWrapper.and(q->q.ge(Car::getMinPrice,50).or().ge(Car::getMaxPrice,50));break;
            }
        }
        PageHelper.startPage(page,pageSize);
        List<Map<String,Object>> cars = carMapper.selectMaps(carLambdaQueryWrapper);
        for (Map<String, Object> car : cars) {
            if (Objects.nonNull(car.get("min_price")) && Objects.nonNull(car.get("max_price"))){
                car.put("price",car.get("min_price")+"-"+car.get("max_price")+"万元");
            }else {
                car.put("price","暂无报价");
            }
            car.remove("min_price");
            car.remove("max_price");
            car.put("salesNum",car.get("sales_num"));
            car.remove("sales_num");
        }

        PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(cars);
        return ResultDto.ok(pageInfo);


    }

    @Override
    public ResultDto<?> getCarInfo(int id,String token) {
        LambdaQueryWrapper<Car> carLambdaQueryWrapper = new LambdaQueryWrapper<>();
        carLambdaQueryWrapper.eq(Car::getId,id)
                .eq(Car::getValidFlag,GlobalVariable.VALID_TRUE);
        Car car = carMapper.selectOne(carLambdaQueryWrapper);

        SysUser sysUser = (SysUser) sysUserService.getUser(token).getData();
        Long userId = sysUser.getId();
        LambdaQueryWrapper<UserCollect> userCollectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userCollectLambdaQueryWrapper.eq(UserCollect::getUserId,userId)
                .eq(UserCollect::getCarId,id)
                .eq(UserCollect::getValidFlag,GlobalVariable.VALID_TRUE);
        UserCollect userCollect = userCollectMapper.selectOne(userCollectLambdaQueryWrapper);
        if (Objects.nonNull(userCollect)){
            car.setCollectFlag("1");
        }

        return ResultDto.ok(car);
    }

    @Override
    public ResultDto<?> getCarModel(int id) {
        LambdaQueryWrapper<CarModel> carModelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        carModelLambdaQueryWrapper.eq(CarModel::getCarId,id)
                .eq(CarModel::getValidFlag,GlobalVariable.VALID_TRUE);
        List<CarModel> carModels = carModelMapper.selectList(carModelLambdaQueryWrapper);

        return ResultDto.ok(carModels);
    }

    @Override
    public ResultDto<?> getCarType() {
        Map<String,Object> brand = new HashMap<>();
        List<String> brandList = carTypeMapper.getBrands();
        brand.put("title","品牌");
        brand.put("list",brandList);
        brand.put("type","brand");

        Map<String,Object> price = new HashMap<>();
        List<String> priceList = carTypeMapper.getPrices();
        price.put("title","价格");
        price.put("list",priceList);
        price.put("type","price");

        Map<String,Object> level = new HashMap<>();
        List<String> levelList = carTypeMapper.getLevels();
        level.put("title","级别");
        level.put("list",levelList);
        level.put("type","level");

        Map<String,Object> energy = new HashMap<>();
        List<String> energyList = carTypeMapper.getEnergies();
        energy.put("title","能源");
        energy.put("list",energyList);
        energy.put("type","energy");

        List<Map<String,Object>> result = new ArrayList<>();
        Collections.addAll(result,brand,price,level,energy);

        return ResultDto.ok(result);
    }

    @Override
    public ResultDto<?> getSaleRank() {
        List<String> carList = carMapper.getSaleRankCar();

        List<Integer> saleNumList = carMapper.getSaleRankSaleNum();

        Map<String,Object> map = new HashMap<>();
        map.put("carList",carList);
        map.put("saleNumList",saleNumList);

        return ResultDto.ok(map);
    }
}
